
import { GetInfoPicture, GetLeaveInfo, GetPreviewImage, PostDeleteData, PostInfoPicture, PostLeaveInfo } from "/constData/requestFun/_httpRequest";
import isEmpty from 'loadsh/isEmpty'
import { POLICIES } from '/constData/_';
import { GetLeave, GetLeaveById, GetLeaveRemaining, LeaveDeleteData, PostLeave, PutLeaveRemaining } from "/constData/requestFun/_teseRequest";
import compact from 'loadsh/compact'
import { CORPID, SPACEID } from "/constData/requestFun/_globalData";
Page({
  options: {
    observers: true
  },
  data: {
    approve: [],
    item: 1,
    min: null,
    max: new Date('2033/12/02'),
    defaultValue: '上午',
    daylist: ['上午', '下午'],
    defaultDate: new Date(),
    arrIndex: 0,
    index: 0,
    leaveinfo: {
      CorpId: '',
      LeaveType: '事假', //请假类型
      AnnualLeave: '', //剩余年假天数
      StartDate: new Date(), //开始时间
      StartDateSuffix: '上午',//上午
      EndDate: new Date(), //结束时间
      EndDateSuffix: '上午',//上午
      SumTime: '0.5', //时长
      ReasonLeave: '', //请假事由
      Annex: '[]', //附件
    },
    displaydata: [],
    sumTime: 0,
    date: 0,
    ReceiveData: [],
    list: ['事假','病假', '年假', '产假', '陪产假', '婚假', '丧假'],
    scrollinfo: [
      {
        title: '发起申请',
        description: '已填写信息，待领导审批',
      },
      {
        title: '领导审批',
        description: '领导审批中',
      },
      {
        title: '审批完成',
        description: '描述信息',
      }
    ],
    LeaveCnt: 0,
    LeaveId: '',
  },
  observers: {
    '**': function (val) {
    }
  },
  Delete(e) {
    const id = e.currentTarget.dataset.id
    LeaveDeleteData(id, (res) => {
      my.alert({
        title: '撤销成功',
      });
      //刷新数据
      this.setData({
        ReceiveData: this.data.ReceiveData.filter((it) => {
          return it.item1.id !== id
        })
      })
    }, (err) => {
      my.alert({
        title: '撤销失败',
      });
    })
  },
  PickerhandleOk(value) {
    this.setData({
      leaveinfo: {
        ...this.data.leaveinfo,
        StartDateSuffix: value,
        SumTime: (DayCompute(this.data.leaveinfo.StartDate, value, this.data.leaveinfo.EndDate, this.data.leaveinfo.EndDateSuffix)).toFixed(1),
      },
      LeaveCnt: (DayCompute(this.data.leaveinfo.StartDate, value, this.data.leaveinfo.EndDate, this.data.leaveinfo.EndDateSuffix)).toFixed(1),
    })
  },
  PickerEndOk(value) {
    this.setData({
      leaveinfo: {
        ...this.data.leaveinfo,
        EndDateSuffix: value,
        SumTime: (DayCompute(this.data.leaveinfo.StartDate, this.data.leaveinfo.StartDateSuffix, this.data.leaveinfo.EndDate, value)).toFixed(1),
      },
      LeaveCnt: (DayCompute(this.data.leaveinfo.StartDate, this.data.leaveinfo.StartDateSuffix, this.data.leaveinfo.EndDate, value)).toFixed(1),
    })

  },

  handleOk1(date, format, e) {
    this.setData({
      leaveinfo: {
        ...this.data.leaveinfo,
        StartDate: date,
        EndDate: date,
      },
      min: date
    })
  },
  handleOk2(date, format, e) {
    this.setData({
      leaveinfo: {
        ...this.data.leaveinfo,
        EndDate: date,
        SumTime: (DayCompute(this.data.leaveinfo.StartDate, this.data.leaveinfo.StartDateSuffix, date, this.data.leaveinfo.EndDateSuffix)).toFixed(1),
      },
      approve: POLICIES.LEAVE_POLICIES(((date - this.data.leaveinfo.StartDate) / 1000 / 60 / 60 / 24).toFixed(2)),
      LeaveCnt: (DayCompute(this.data.leaveinfo.StartDate, this.data.leaveinfo.StartDateSuffix, date, this.data.leaveinfo.EndDateSuffix)).toFixed(1)
    })
  },

  handleControlledOk(value) {
    this.setData({
      leaveinfo: {
        ...this.data.leaveinfo,
        LeaveType: value,
      }
    })
    GetLeaveRemaining(value, this.data.leaveinfo.CorpId, (res) => {
      this.setData({
        leaveinfo: {
          ...this.data.leaveinfo,
          AnnualLeave: res.data.cnt.toString()
        },
        LeaveId: res.data.id.toString()
      })
    }, (err) => {

    })
  },
  // onannualChange(e) {
  //   this.setData({
  //     leaveinfo: {
  //       ...this.data.leaveinfo,
  //       AnnualLeave: e
  //     }
  //   })
  // },
  onvehicleChange(e) {
    this.setData({
      leaveinfo: {
        ...this.data.leaveinfo,
        vehicle: e
      }
    })
  },
  onreasonChange(e) {
    this.setData({
      leaveinfo: {
        ...this.data.leaveinfo,
        ReasonLeave: e
      }
    })
  },
  handleChange(value) {
    this.setData({
      value,
    });
  },
  onLoad() {
    my.getStorage({
      key: 'userInfo',
      success: (storage) => {
        GetLeave((res) => {
          const cacheData = res.data.map(it => {
            if (it.item1.corpId === storage.data.result.userid) {
              it.item1['displayAnnex'] = JSON.parse(it.item1.annex)
              return it
            }
          })
          this.setData({
            leaveinfo: {
              ...this.data.leaveinfo,
              CorpId: storage.data.result.userid
            },
            ReceiveData: compact(cacheData).reverse()
          })
        })
      }
    })
  },
  // 上传数据
  onSubmit(e) {
    console.log('数据',JSON.stringify(this.data.leaveinfo));
    PostLeave(JSON.stringify(this.data.leaveinfo),
      (res) => {
        my.alert({
          title: '提交成功',
        });
        new Promise((resolve) => {
          PutLeaveRemaining(this.data.LeaveId, JSON.parse(this.data.LeaveCnt), (PutRes) => {
            resolve('假期修改成功')
          })
        }).then((ThenRes) => {
          GetLeaveById(res.data, (res) => {
            this.setData({
              ReceiveData: compact([...this.data.ReceiveData, res.data].map(it => {
                if (it.item1.corpId === this.data.leaveinfo.CorpId) {
                  it.item1['displayAnnex'] = JSON.parse(it.item1.annex)
                  return it
                }
              })).reverse()
            })
          })
        })
        this.setData({
          leaveinfo: {
            ...this.data.leaveinfo,
            LeaveType: '事假', //请假类型
            AnnualLeave: '', //剩余年假天数
            StartDate: null, //开始时间
            StartDateSuffix: '上午',//上午
            EndDate: null, //结束时间
            EndDateSuffix: '上午',//上午
            SumTime: 0.5, //时长
            ReasonLeave: '', //请假事由
            Annex: '[]', //附件
          }
        })
      },
      (err) => {
        my.alert({
          title: '提交成功1',
        });
      })
  },
  //上传图片
  onUpload() {
    let imageAnnex = ''
    dd.uploadAttachmentToDingTalk({
      image: {
        max: 9,
        spaceId: SPACEID,
        compress: true,
        folderId: '',
        multiple: true,
      },
      types: ['photo'],
      success: (res) => {
        const srcAnnex = this.data.leaveinfo.Annex
        imageAnnex = res.data
        imageAnnex.push(...JSON.parse(srcAnnex))

        this.setData({
          displaydata: imageAnnex,
          leaveinfo: {
            ...this.data.leaveinfo,
            Annex: JSON.stringify(imageAnnex)
          }
        })
      },
      fail: () => { },
      complete: () => { },
    });

  },

  PreviewBtn(e) {
    const item = e.currentTarget.dataset.item
    dd.previewFileInDingTalk({
      spaceId: item.spaceId,
      fileName: item.fileName,
      corpId: CORPID,
      fileSize: item.fileSize,
      fileType: item.fileType,
      fileId: item.fileId,
    });
  },
  CheckPictures(e) {
    const item = e.currentTarget.dataset.item
    dd.previewFileInDingTalk({
      spaceId: item.spaceId,
      fileName: item.fileName,
      corpId: CORPID,
      fileSize: item.fileSize,
      fileType: item.fileType,
      fileId: item.fileId,
    });
  },
  //获取数据


});

function DayCompute(StartDate, StartDateSuffix, EndDate, EndDateSuffix) {
  if (StartDateSuffix === EndDateSuffix) {
    return Math.ceil(((EndDate - StartDate) / 1000 / 60 / 60 / 24)) + 0.5;
  }
  else {
    if (StartDateSuffix === '上午' && EndDateSuffix === '下午') {
      return Math.ceil(((EndDate - StartDate) / 1000 / 60 / 60 / 24)) + 1;
    }
    if (StartDateSuffix === '下午' && EndDateSuffix === '上午') {
      return Math.ceil(((EndDate - StartDate) / 1000 / 60 / 60 / 24));
    }
  }
}

